<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class AdminBaseController extends Controller {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = 'main';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    public $logged = 0; //此次请求是否已经做日志记录,0为未做
    public $userType = 1; //用户类型，1表示为后台用户，2表示前台用户，0表示未知
    public $areaCode;//当前操作员的地区编号
    
    public function init() {
    	Yii::app()->request->enableCookieValidation	= false;
        parent::init();
		    /*echo '<script type="text/javascript">alert("对不起，后台正在维护，暂时不可登录和操作，请稍晚再试");top.location="http://www.dld.com"</script>';
		    exit;*/
        //$this->areaCode=Yii::app()->admin->getStorage('area_code');
    }

    public function filters() {
        return array(
        	'RequestI',
        	'Decrypt + myPwdEdit',
        	'Permission',
        );
    }

    

    /*     * 将所有xxtea加密算法加密的$_POST请求还原,支持到四维数组* */

    public function filterDecrypt($filterChain) {
        if (count($_POST)) {
            foreach ($_POST as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $kkk => $vvv) {
                        if (is_array($vvv)) {
                            foreach ($vvv as $kk => $vv) {
                                if (is_array($vv)) {
                                    foreach ($vv as $k => $v) {
                                        if (is_array($v)) {
                                            throw new CHttpException(403, '不正确的请求');
                                        } else {
                                            $tv = $XxteaD::decrypt(trim($v), Yii::App()->params['adminpassportkey']);
                                            if ($tv) {
                                                $_POST[$key][$kkk][$kk][$k] = $tv;
                                            }
                                        }
                                    }
                                } else {
                                    $tv = XxteaD::decrypt(trim($vv), Yii::App()->params['adminpassportkey']);
                                    if ($tv) {
                                        $_POST[$key][$kkk][$kk] = $tv;
                                    }
                                }
                            }
                        } else {
                            $tv = XxteaD::decrypt(trim($vvv), Yii::App()->params['adminpassportkey']);
                            if ($tv) {
                                $_POST[$key][$kkk] = $tv;
                            }
                        }
                    }
                } else {
                    $tv = XxteaD::decrypt(trim($value), Yii::App()->params['adminpassportkey']);
                    if ($tv) {
                        $_POST[$key] = $tv;
                    }
                }
            }
        }
        $filterChain->run();
    }

    /**
     * 权限检查
     * @author zjx
     * @throws CHttpException
     */
    public function filterPermission($filterChain) {
        $route = $this->getRouteI();
        $route = strtolower($route);
        if ($route === 'admin/attachment/upload') {
            $sessionId = Yii::app()->request->getParam(session_name(), false);
            if ($sessionId) {
                session_id($sessionId);
            } else {
                //$this->redirect('?r=admin/user/login');
				echo '<script type="text/javascript">window.top.location = "?r=admin/user/login";</script>';
				Yii::app()->end();
            }
        }
        $this->areaCode=Yii::app()->admin->getStorage('area_code');
        if (Yii::app()->admin->isGuest) {
        	//例外检查
            if (!(in_array($route, array_keys(Yii::app()->params['allowedTask'])) && isset(Yii::app()->params['allowedTask'][$route]['needLogin']) && !Yii::app()->params['allowedTask'][$route]['needLogin'])) {
            	//$this->redirect('?r=admin/user/login');
				echo '<script type="text/javascript">window.top.location = "?r=admin/user/login";</script>';
				Yii::app()->end();
            }
        } else {
        	if(!UtilD::isPass($route)){
        		throw new CHttpException(403, ' 您没有权限访问此网页');
        	}
        }
        $filterChain->run();
    }
}